<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="完成"
  title="设备下发"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  :confirm-loading="loadingMap.batchEquipmentIssuance"
  size="small"
)
  .distribute-equipment
    SectionTitle 已选标本
    Table(
      height="auto"
      :columns="tableColumns"
      :data="selectionRow"
    )
    SectionTitle 选择设备
    el-form
      el-form-item(label="选择")
        mt-select(v-if ="appmodel" v-bind="selectEquipmentProps" label="选择设备" v-model="equipment")
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  SectionTitle,
  Table,
  useNotify,
  type SelectProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { ESpeimenTableColumnKey, specimenColumns } from "@scmt/api-common";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const equipment = ref("");
const equipmentName = ref("");
const specimenList = ref([]);

const tableColumns = computed(() => {
  return [
    specimenColumns[ESpeimenTableColumnKey.Barcode],
    specimenColumns[ESpeimenTableColumnKey.State],
    specimenColumns[ESpeimenTableColumnKey.PatientName],
    specimenColumns[ESpeimenTableColumnKey.Sex],
    specimenColumns[ESpeimenTableColumnKey.Age],
    specimenColumns[ESpeimenTableColumnKey.SpecimenTypeId],
  ];
});

/**
 * 设备选择下拉框属性
 */
const selectEquipmentProps = computed<SelectProps>(() => {
  const props: SelectProps = {
    loadMethod: () =>
      $api.lis.specimen
        .listAsOption({ specimenList: specimenList.value })
        .then((res) => {
          equipment.value = res[0]?.value || "";
          return res;
        }) ?? [],
    placeholder: "请选择设备",
  };

  return props;
});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const select = (val: any) => {
  if (!val) {
    equipment.value = val.value;
    equipmentName.value = val.label;
  }
};

const { data, request, loadingMap } = useRequest({
  batchEquipmentIssuance: (params: any) => {
    return $api.lis.specimen.batchEquipmentIssuance(params).then(() => {
      $notify.success("下发成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
});

const updateConfirmModel = () => {
  if (!equipment.value) {
    $notify.error("请选择设备");
    return;
  }
  request("batchEquipmentIssuance", {
    specimenList: props.selectionRow,
    deviceId: equipment.value,
    deviceName: equipmentName.value,
  });
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      equipment.value = "";
      editingObj.value = {};
      const applicationProjectList = []
        .concat(props.selectionRow)
        .map((item) => {
          return item.applicationProjectId;
        });
      if (applicationProjectList.length == 1) {
        if (applicationProjectList[0].indexOf("|") > -1) {
          specimenList.value = applicationProjectList[0].split("|");
        } else {
          specimenList.value = applicationProjectList;
        }
      } else {
        for (let i = 0; i < applicationProjectList.length; i++) {
          if (applicationProjectList[i].indexOf("|") > -1) {
            let data = applicationProjectList[i].split("|");
            for (let j = 0; j < data.length; j++) {
              specimenList.value.push(data[j]);
            }
          } else {
            specimenList.value.push(applicationProjectList[i]);
          }
        }
      }
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.distribute-equipment {
  display: flex;
  flex-direction: column;
}
</style>
